package com.zuoqi.easywork.projectManage.controller;

import com.github.pagehelper.Page;
import com.zuoqi.common.utils.IMap;
import com.zuoqi.common.utils.PageView;
import com.zuoqi.common.utils.ReturnMapUtils;
import com.zuoqi.common.utils.SessionUtil;
import com.zuoqi.easywork.projectManage.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zuoqi.common.utils.ReturnMapUtils.businessResult;

@Controller
public class projectUserManage {
    @Autowired
    private ProjectService projectService;

    @RequestMapping(value = "/projectUserManage/toPage/projectManageView")
    public String projectManageView(){
        return "/module/projectManage/projectManage1";
    }
    /**
     * 获得项目列表
     * */
    @RequestMapping(value = "/projectUserManage/ajax/getProjectList",method = RequestMethod.POST)
    @ResponseBody
    public Map getProjectList(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userDepart = SessionUtil.getLoginUserDepart(session);
        params.put("iDepart",userDepart);
        Page page = PageView.startPageForTable(params, false);
        List<IMap> projectList = projectService.getProjectListForManage(params);
        Map<String, Object> result = PageView.getResultForTable(page, projectList);
        return result;
    }
    /**
     * 获得项目详细信息
     * */
    @RequestMapping(value = "/projectUserManage/toPage/getProjectInfo")
    public String getProjectInfo(HttpServletRequest request,ModelMap modelMap){
        IMap params = new IMap(request);
        IMap thisProject = projectService.getProjactInfo(params.getString("proId"));
        modelMap.addAttribute("thisProject",thisProject);
        return "/module/projectManage/projectInfo";
    }

    /**
     * 获取可选择部员列表
     * */
    @RequestMapping(value = "/projectUserManage/ajax/selectDepartUser")
    @ResponseBody
    public Map<String, Object>  selectDepartUser(HttpServletRequest request) {
        IMap param = new IMap(request);
        HttpSession session = request.getSession();
        String userDepart = SessionUtil.getLoginUserDepart(session);
        param.put("depart",userDepart);
        Page page = PageView.startPageForTable(param, false);
        List<IMap> list = projectService.selectDepartUser(param);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",list);
        return result;
    }
    /**
     * 为项目分配人员
     * */
    @RequestMapping(value = "/projectUserManage/ajax/addProjectUser")
    @ResponseBody
    public Map  addProjectUser(HttpServletRequest request) {
        IMap param = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        param.put("updateUser",userId);
        int ret = projectService.addProjectUser(param);
        if (1==ret){
            return ReturnMapUtils.businessResult("0000","设置成功！");
        }else if (0==ret){
            return ReturnMapUtils.businessResult("0000","此项目人员已清空！");
        }else {
            return ReturnMapUtils.businessResult("8888","操作失败！");
        }
    }
}
